import Vue from 'vue';
import axios from 'axios';
import router from '../router/index.js';

const request = axios.create({
  //   创建一个新的axios
  baseURL: 'http://127.0.0.1:8888/api/private/v1/',
});

request.interceptors.request.use((config) => {
  if (!config.url.startsWith('/login')) {
    config.headers.Authorization = localStorage.getItem('token');
  }
  return config;
});

request.interceptors.response.use((res) => {
  if (res.data.meta.status === 400 && res.data.meta.msg === '无效token') {
    // window.localtion.href="http://localhost:8080/#/login";
    router.push('/login');
  }
  return res;
});

Vue.prototype.__apis = {
  login(data) {
    return request({
      method: 'post',
      url: '/login',
      data,
    });
  },
  getLeftMenus() {
    return request({
      method: 'get',
      url: '/menus',
      // headers: { Authorization: localStorage.getItem('token') },
    });
  },
  getUsers(data) {
    return request({
      method: 'get',
      url: '/users',
      // headers: { Authorization: localStorage.getItem('token') },
      params: data,
    });
  },
  // 更改用户状态
  putState(data) {
    return request({
      method: 'put',
      url: '/users/' + data.uid + '/state/' + data.type,
    });
  },
  // 添加用户
  postUsers(data) {
    return request({
      method: 'post',
      url: '/users',
      data,
    });
  },
  // 删除单个用户
  deleteUsers(data) {
    return request({
      method: 'delete',
      url: '/users/' + data.id,
    });
  },
  // 获取角色列表
  getRoles() {
    return request({
      method: 'get',
      url: '/roles',
    });
  },
  // 分配用户角色
  putRoles(data) {
    return request({
      method: 'put',
      url: '/users/' + data.id + '/role',
      data: {
        rid: data.rid,
      },
    });
  },
  // 修改用户数据
  putUsers(data) {
    return request({
      method: 'put',
      url: '/users/' + data.id,
      data: {
        email: data.email,
        mobile: data.mobile,
      },
    });
  },

  // 角色相关
  roles: {
    // 添加角色
    postRoles(data) {
      return request({
        method: 'post',
        url: '/roles',
        data,
      });
    },
    // 修改角色
    editRoles(data) {
      return request({
        method: 'put',
        url: '/roles/' + data.id,
        data: {
          roleName: data.roleName,
          roleDesc: data.roleDesc,
        },
      });
    },
    // 删除角色
    deleteRoles(id) {
      return request({
        method: 'delete',
        url: '/roles/' + id,
      });
    },
  },

  // 权限相关
  rights: {
    // 获取权限列表
    getRights(data) {
      return request({
        method: 'get',
        url: `/rights/${data}`,
      });
    },
    // 修改角色权限
    postRights(data) {
      return request({
        method: 'post',
        url: '/roles/' + data.roleId + '/rights',
        data: {
          rids: data.rids,
        },
      });
    },
    // 删除角色指定权限
    deleteRight(roleId, rightId) {
      // console.log(roleId, rightId);
      return request({
        method: 'delete',
        url: `/roles/${roleId}/rights/${rightId}`,
      });
    },
  },

  // 商品管理相关
  goods: {
    // 商品列表数据
    getGoods(data) {
      return request({
        method: 'get',
        url: '/goods',
        params: {
          query: data.query,
          pagenum: data.pagenum,
          pagesize: data.pagesize,
        },
      });
    },
    // 商品分类数据列表
    getCategoriesList(data) {
      return request({
        method: 'get',
        url: '/categories',
        params: {
          type: data.type,
          pagenum: data.pagenum,
          pagesize: data.pagesize,
        },
      });
    },
    // 添加商品分类
    addCategories(data) {
      return request({
        method: 'post',
        url: '/categories',
        data,
      });
    },
    // 编辑提交分类
    putCategories(data) {
      return request({
        method: 'put',
        url: `categories/${data.cat_id}`,
        data: {
          cat_name: data.cat_name,
        },
      });
    },
    // 删除分类
    deleteCategories(id) {
      return request({
        method: 'delete',
        url: `categories/${id}`,
      });
    },
    // 添加商品
    postGoods(data) {
      // console.log(data);
      return request({
        method: 'post',
        url: '/goods',
        data: {
          goods_name: data.goods_name,
          goods_weight: data.goods_weight,
          goods_number: data.goods_number,
          goods_cat: data.goods_cat, // 字符串
          goods_price: data.goods_price,
          goods_introduce: data.goods_introduce,
          pics: data.pics,
        },
      });
    },
    // 删除商品
    deleteGoods(id) {
      return request({
        method: 'delete',
        url: `goods/${id}`,
      });
    },
  },

  // 分类参数相关
  attributes: {
    getAttributes(data) {
      return request({
        method: 'get',
        url: `categories/${data.id}/attributes`,
        params: {
          sel: data.sel,
        },
      });
    },
    // 添加动态参数或静态属性
    postAttributes(data) {
      return request({
        method: 'post',
        url: `categories/${data.id}/attributes`,
        data: {
          attr_name: data.attr_name,
          attr_sel: data.attr_sel,
          attr_vals: data.attr_vals,
        },
      });
    },
    // 修改参数属性
    putAttributes(data) {
      return request({
        method: 'put',
        url: `/categories/${data.id}/attributes/${data.attrId}`,
        data: {
          attr_name: data.attr_name,
          attr_sel: data.attr_sel,
          attr_vals: data.attr_vals,
        },
      });
    },
    // 删除参数
    deleteAttributes(data) {
      return request({
        method: 'delete',
        url: `/categories/${data.id}/attributes/${data.attrid}`,
      });
    },
  },

  // 订单相关
  orders: {
    // 获取订单数据列表
    getOrders(data) {
      return request({
        method: 'get',
        url: '/orders',
        params: {
          query: data.query,
          pagenum: data.pagenum,
          pagesize: data.pagesize,
          user_id: data.user_id,
          pay_status: data.pay_status,
          is_send: data.is_send,
          order_fapiao_title: data.order_fapiao_title,
          order_fapiao_company: data.order_fapiao_company,
          order_fapiao_content: data.order_fapiao_content,
          consignee_addr: data.consignee_addr,
        },
      });
    },
    // 查看物流信息
    getKuaidi() {
      return request({
        method: 'get',
        url:
          'https://www.liulongbin.top:8888/api/private/v1/kuaidi/1106975712662',
      });
    },
  },

  // 数据统计
  getReports() {
    return request({
      method: 'get',
      url: '/reports/type/1',
    });
  },
};
